package com.ruoyi.hospital.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.hospital.domain.YlArea;
import com.ruoyi.hospital.service.IYlAreaService;
import com.ruoyi.hospital.util.AddressLngLatExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 院区信息Controller
 * 
 * @author ruoyi
 * @date 2022-02-16
 */
@RestController
@RequestMapping("/area")
public class YlAreaController extends BaseController
{
    @Autowired
    private IYlAreaService ylAreaService;

    /**
     * 查询院区信息列表
     */
    @RequiresPermissions("hospital:area:list")
    @GetMapping("/list")
    public TableDataInfo list(YlArea ylArea)
    {
        startPage();
        List<YlArea> list = ylAreaService.selectYlAreaList(ylArea);
        AddressLngLatExchange addressLngLatExchange = new AddressLngLatExchange();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setCenter(addressLngLatExchange.getLngLat(list.get(i).getAddress()));
        }
        return getDataTable(list);
    }

    /**
     * 查询所有院区信息
     */
    @GetMapping("/arealist")
    public TableDataInfo getList()
    {
        List<YlArea> ylAreaList = ylAreaService.selectAll();
        return getDataTable(ylAreaList);
    }

    /**
     * 导出院区信息列表
     */
    @RequiresPermissions("hospital:area:export")
    @Log(title = "院区信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YlArea ylArea)
    {
        List<YlArea> list = ylAreaService.selectYlAreaList(ylArea);
        ExcelUtil<YlArea> util = new ExcelUtil<YlArea>(YlArea.class);
        util.exportExcel(response, list, "院区信息数据");
    }

    /**
     * 获取院区信息详细信息
     */
    @RequiresPermissions("hospital:area:query")
    @GetMapping(value = "/{areaId}")
    public AjaxResult getInfo(@PathVariable("areaId") Long areaId)
    {
        return AjaxResult.success(ylAreaService.selectYlAreaByAreaId(areaId));
    }

    /**
     * 获取院区信息详细经纬度
     */
    @GetMapping(value = "/address/{address}")
    public AjaxResult getXY(@PathVariable("address") String address)
    {
        AddressLngLatExchange addressLngLatExchange = new AddressLngLatExchange();
        return AjaxResult.success(addressLngLatExchange.getLngLat(address));
    }

    /**
     * 新增院区信息
     */
    @RequiresPermissions("hospital:area:add")
    @Log(title = "院区信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YlArea ylArea)
    {
        return toAjax(ylAreaService.insertYlArea(ylArea));
    }

    /**
     * 修改院区信息
     */
    @RequiresPermissions("hospital:area:edit")
    @Log(title = "院区信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YlArea ylArea)
    {
        return toAjax(ylAreaService.updateYlArea(ylArea));
    }

    /**
     * 删除院区信息
     */
    @RequiresPermissions("hospital:area:remove")
    @Log(title = "院区信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{areaIds}")
    public AjaxResult remove(@PathVariable Long[] areaIds)
    {
        return toAjax(ylAreaService.deleteYlAreaByAreaIds(areaIds));
    }
}
